package com.ficv.findme;

import org.apache.cordova.DroidGap;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.WebView;

public class JavascriptFacade implements Cloneable {

		  private WebView mAppView;
		  private DroidGap mGap;
		  
		  public static String DATA="30000";
		  public static long delay=30000;
		  
		  
		  public long getDelay() {
			return  JavascriptFacade.delay;
		}

		public long setDelay(long delay) {
			Log.v("JavascriptFacade","setting delay: " + delay);
			JavascriptFacade.delay = delay;
			return JavascriptFacade.delay;
		}

		public  String getData() {
			return DATA;
		}

		public   String setData(String data) {
			Log.v(JavascriptFacade.class.getName(), "setting data " + data);
			DATA = data;
//			SharedPreferences prefs = mGap.getSharedPreferences(
//				      "com.ficv.findme", Context.MODE_PRIVATE);
//			
//			prefs.edit().putString("com.ficv.findme.data", data);
//			
			return DATA;
		}

		public JavascriptFacade(DroidGap gap, WebView view)
		  {
		    mAppView = view;
		    mGap = gap;
		  }

		  public String getTelephoneNumber(){
		    TelephonyManager tm = 
		      (TelephonyManager) mGap.getSystemService(Context.TELEPHONY_SERVICE);
		    String number = tm.getLine1Number() + tm.getNetworkCountryIso() + tm.getNetworkOperator() + tm.getSimOperator() + tm.getSimSerialNumber();
		    return number;
		  }
		  
		  public String getImei(){
  
			  TelephonyManager telephonyManager = (TelephonyManager)mGap.getSystemService(Context.TELEPHONY_SERVICE);
			  return telephonyManager.getDeviceId();
		  }
		  
		  
		  public boolean getGpsStatus(){
			  
			   LocationManager locationManager = (LocationManager) mGap.getSystemService(MainActivity.LOCATION_SERVICE);
			    		    	
			   return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		  }		  
		  
		  
		public  String testData(String data) {
				
			return "echo: " + data;
		}
		
		public  int stopService() {
			
			int result=-1;
			
			try {
				mGap.stopService(new Intent(GpsService.class.getName()));
				result=1;
			} catch(Throwable t) {
				Log.e(JavascriptFacade.class.getName(), t.getMessage() + ": " + t.getCause());
			}
			
			return result;
		}
		
		public  int cleanNotification() {
			
			int result=-1;
			
			try {
			    String ns = Context.NOTIFICATION_SERVICE;
			    NotificationManager nMgr = (NotificationManager) mGap.getSystemService(ns);
			    nMgr.cancel(FindMeService.NOTIFICATION_ID);
			    nMgr.cancelAll();
				result=1;
			} catch(Throwable t) {
				Log.e(JavascriptFacade.class.getName(), t.getMessage() + ": " + t.getCause());
			}
			
			return result;
		}	
		
		

		
//		public String getTxt() {
//			
//			return ((MainActivity)mGap).getFindMeServ().ºgetTxt();
//		}
		
	
}
